import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.linear_model import LinearRegression
plt.rcParams['font.sans-serif'] = 'SimHei'

df = pd.read_excel('最新发布的北京二手房数据_预处理.xlsx')
unit_price = df['单价（元/平方米）']
house_area = df['面积（平方米）']
house_type = df[['室','厅']]
house_regin = df[['通州','朝阳','昌平','顺义','丰台','海淀','西城','房山','石景山','大兴','怀柔','东城','门头沟','密云','延庆','平谷','亦庄开发区']]
house_finish = df[['毛坯','简装','精装']]
house_structure = df[['塔楼','板楼','板塔结合','平房']]
is_subway = df[['近地铁','不近地铁']]
house_dirt = df[['东','南','西','北','东北','东南','西南','西北']]
house_year = df['房龄']
x = pd.concat([house_area,house_type,house_regin,house_finish,house_structure,is_subway,house_dirt,house_year],axis=1)
y = unit_price

x_train,x_test,y_train,y_test=model_selection.train_test_split(x,y,test_size=0.2)
LR = LinearRegression()
reg = LR.fit(x_train,y_train)
predicted = reg.predict(x_test)

plt.figure(figsize=(12,6))
n = 50
plt.plot(range(n),y_test[-n:],'-.*')
plt.plot(range(n),predicted[-n:],'r--.')
plt.legend(['实际值','估计值'])
plt.xlabel('后50个数据')
plt.ylabel('单价/(元/平方米)')
plt.title('二手房房价实际值和估计值折线图')
plt.show()

x_test.loc[len(x_test)] = [80, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 15]
#80平方米、2室1厅、昌平、精装、塔楼、靠近地铁、朝南、15年
x_test.loc[len(x_test) + 1] = [80, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 15]
predicted = reg.predict(x_test)
print('80平方米、2室1厅、海淀、精装、塔楼、靠近地铁、朝南、15年的房价预测值（元/平方米）：\n', predicted[-2])
print('80平方米、2室1厅、昌平、精装、塔楼、靠近地铁、朝南、15年的房价预测值（元/平方米）：\n', predicted[-1])
